package scau.hotel.hotel_service.service.impl;

import org.springframework.stereotype.Service;
import scau.hotel.hotel_model.redis.RedisCache;
import scau.hotel.hotel_service.entity.Book;
import scau.hotel.hotel_service.entity.RoomType;
import scau.hotel.hotel_service.mapper.BookMapper;
import scau.hotel.hotel_service.service.BookService;
import scau.hotel.hotel_service.service.PaymentService;
import scau.hotel.hotel_service.service.RoomTypeService;
import scau.hotel.hotel_service.vo.BookVo;
import scau.hotel.hotel_service.vo.CheckinCusInforVo;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_model.controller.RespBeanEnum;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Book)表服务实现类
 *
 * @author makejava
 * @since 2022-04-22 13:36:13
 */
@Service("bookService")
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;

    /**
     * 服务对象
     */
    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 服务对象
     */
    @Resource
    private PaymentService paymentService;

    /**
     * 服务对象
     */
    @Resource
    private CheckinInforServiceImpl checkinInforServiceImpl;


    @Resource
    private RedisCache redisCache;

    /**
     * 通过ID查询单条数据
     *
     * @param bookId 主键
     * @return 实例对象
     */
    @Override
    public Book queryById(Integer bookId) {
        return this.bookMapper.queryById(bookId);
    }

    /**
     * 查询所有
     *
     * @param book 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Book> queryAll(Book book) {
        return this.bookMapper.queryAll(book);
    }

    /**
     * 重复调用方法：格式化时间
     *
     * @return 正确时间格式
     */
    public Date formatDate(String time) {
        //格式化时间
        time = time.replace("/", "-");//把带时分秒的 time1 转为 yyyy-MM-dd 格式的字符串
        time = time.replace("undefined", "01");//默认为1号开始，预防如 2022-05-undefined 的情况
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date newTime = new Date();
        try {
            newTime = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return newTime;
    }

    /**
     * 查询所有并返回折扣价格
     *
     * @param map 筛选条件
     * @return 查询结果
     */
    @Override
    public List<RoomType> queryAllAndGetPrice(Map<Object, Object> map) {
        //格式化时间
        String time1 = (String) map.get("checkinDate");
        String time2 = (String) map.get("checkoutDate");
        Date newTime1 = formatDate(time1);
        Date newTime2 = formatDate(time2);
        RoomType roomType = new RoomType();
        List<RoomType> roomTypeList = this.roomTypeService.queryAll(roomType);
        for (RoomType roomType1:roomTypeList) {
            roomType1.setRoomTypePrice((BigDecimal)this.paymentService.getPrice(newTime1,newTime2,roomType1.getRoomTypeId()).getData());
        }
        return roomTypeList;
    }

    /**
     * 小程序端新增数据
     *
     * @param map 实例对象
     * @return RespBean
     */
    @Override
    public RespBean insert(Map<Object, Object> map) {
        Book book = new Book();
        book.setBookNum((Integer) map.get("bookNum"));
        //格式化时间
        String time1 = (String) map.get("checkinDate");
        String time2 = (String) map.get("checkoutDate");
        Date newTime1 = formatDate(time1);
        Date newTime2 = formatDate(time2);
        //设置预计入住时间
        book.setCheckinDate(newTime1);
        //设置预计退房时间
        book.setCheckoutDate(newTime2);
        //获取预订人名字
        book.setCusName((String) map.get("cusName"));
        //获取预订人身份证
        book.setCusIdcard((String) map.get("cusIdcard"));
        //获取预订人电话
        book.setCusTel((String) map.get("cusTel"));
        //获取备注
        book.setRemark((String) map.get("remark"));
        //获取预定的房型id
        book.setRoomTypeId((Integer) map.get("roomTypeId"));
        //设置删除标志，默认存在为1，删除为0
        book.setDelFlag(1);
        //设置预定表状态
        book.setStatus("未入住");
        //插入数据库
        this.bookMapper.insert(book);
        //返回结果
        return RespBean.success();
    }

    /**
     * web端新增数据
     *
     * @param book 实例对象
     * @return RespBean
     */
    @Override
    public RespBean insertByWeb(Book book) {
        book.setDelFlag(1);
        book.setStatus("未入住");
        this.bookMapper.insert(book);
        return RespBean.success();
    }

    /**
     * 修改数据
     *
     * @param book 实例对象
     * @return RespBean
     */
    @Override
    public RespBean update(Book book) {
        this.bookMapper.update(book);
        return RespBean.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param bookId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer bookId) {
        return this.bookMapper.deleteById(bookId) > 0;
    }

    /**
     * 办理入住自动登记预订人信息
     * @param bookVo
     * @return
     */
    @Override
    public RespBean autoDoRegistered(BookVo bookVo) {
        if(this.redisCache.getCacheList("rCusInfor").size()==0) {
            this.redisCache.setCacheObject("bookId",bookVo.getBookId());
            //转换成CheckinCusInforVo对象
            CheckinCusInforVo checkinCusInforVo = new CheckinCusInforVo();
            checkinCusInforVo.setCusName(bookVo.getCusName());
            checkinCusInforVo.setCusIdcard(bookVo.getCusIdcard());
            checkinCusInforVo.setCusTel(bookVo.getCusTel());
            String remark = bookVo.getRemark();
            if (remark == null) {
                checkinCusInforVo.setRemark(this.roomTypeService.queryById(bookVo.getRoomTypeId()).getRoomTypeName());
            } else {
                checkinCusInforVo.setRemark(this.roomTypeService.queryById(bookVo.getRoomTypeId()).getRoomTypeName() + "，" + remark);
            }
            //把CheckinCusInforVo对象添加进数组，调用doRegistered方法
            List<CheckinCusInforVo> checkinCusInforVos = new ArrayList<>();
            checkinCusInforVos.add(checkinCusInforVo);
            RespBean result = this.checkinInforServiceImpl.doRegistered(checkinCusInforVos);
            //返回成功结果，取得对应房间名称并提示
            if (result.getCode() == 200) {
                return RespBean.success("客人信息已成功录入，请选择" +
                        "“" + this.roomTypeService.queryById(bookVo.getRoomTypeId()).getRoomTypeName() + "”"
                        + "入住");
            } else {
                return result;
            }
        }else{
            return RespBean.error(RespBeanEnum.FORBIDDEN_ERROR,"请先为已登记的客人办理入住！");
        }
    }
}
